package com.oa.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonController;
import com.joneying.common.web.response.Resp;
import com.oa.core.BaseController;
import com.oa.core.PageResult;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.dto.ApprovalProcessAddDTO;
import com.oa.system.dto.MyApprovalListDTO;
import com.oa.system.entity.ApprovalNode;
import com.oa.system.entity.ApprovalPlan;
import com.oa.system.entity.ProcessInfoSubmit;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IApprovalNodeService;
import com.oa.system.service.IApprovalPlanService;
import com.oa.system.service.IApprovalProcessDefineService;
import com.oa.system.vo.MyApprovalListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(description = "流程定义接口")
@RequestMapping("/approvalProcessDefine")
@RestController
public class ApprovalProcessDefineController extends BaseController {

    @Autowired
    private IApprovalProcessDefineService approvalProcessDefineService;

    @Autowired
    private IApprovalNodeService approvalNodeService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IApprovalPlanService approvalPlanService;

    @GetMapping("/my/find")
    @CommonController
    @Description("我发起")
    @ApiOperation(value = "我发起")
    public Resp<PageResult<MyApprovalListVO>> myApprovalFindUserId(MyApprovalListDTO myApprovalListDTO){
        myApprovalListDTO.setUserId(getLoginUserId());
        return new Resp().success(approvalProcessDefineService.myApprovalFindUserId(myApprovalListDTO));
    }

    @GetMapping("/copy/record")
    @CommonController
    @Description("抄送我")
    @ApiOperation(value = "抄送我")
    public Resp<PageResult<MyApprovalListVO>> findCopyRecord(MyApprovalListDTO myApprovalListDTO){
        myApprovalListDTO.setUserId(getLoginUserId());
        return new Resp().success(approvalProcessDefineService.findCopyRecord(myApprovalListDTO));
    }

    @GetMapping("/approval/all/list")
    @CommonController
    @Description("审批的(所有)")
    @ApiOperation(value = "审批的(所有)")
    public Resp<PageResult<MyApprovalListVO>> allApproval(MyApprovalListDTO myApprovalListDTO){
        myApprovalListDTO.setUserId("");
        return new Resp().success(approvalProcessDefineService.myApproval(myApprovalListDTO));
    }

    @GetMapping("/myApproval")
    @CommonController
    @Description("我审批的")
    @ApiOperation(value = "我审批的")
    public Resp<PageResult<MyApprovalListVO>> myApproval(MyApprovalListDTO myApprovalListDTO){
        myApprovalListDTO.setUserId(getLoginUserId());
        return new Resp().success(approvalProcessDefineService.myApproval(myApprovalListDTO));
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp findDetailsById(@PathVariable String id) {
        return new Resp().success(approvalProcessDefineService.details(id));
    }

    @PostMapping("/save")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp save(@RequestBody @Valid ApprovalProcessAddDTO approvalProcessAddDTO) throws DataIsEmptyException {
        return approvalProcessDefineService.add(approvalProcessAddDTO);
    }

    @GetMapping("/default/plan/node/list/{approvalProcessId}")
    @CommonController
    @Description("查询默认方案")
    @ApiOperation(value = "查询默认方案")
    public Resp<List<ApprovalPlan>> getDefaultPlanNodeList(@PathVariable String approvalProcessId){
        ApprovalPlan approvalPlan = approvalPlanService.selectOne(new EntityWrapper().eq("approval_process_id", approvalProcessId).eq("is_default", "1"));
        return new Resp().success(approvalPlan);
    }

    @GetMapping("/node/list/{planId}")
    @CommonController
    @Description("根据方案查询节点")
    @ApiOperation(value = "根据方案查询节点")
    public Resp<List<ApprovalNode>> getApprovalNodeListPlanId(@PathVariable Long planId){
        List<ApprovalNode> approvalNodes = approvalNodeService.selectList(new EntityWrapper().eq("approval_plan_id", planId));
        return new Resp().success(approvalNodes);
    }

    @GetMapping("/plan/id")
    @CommonController
    @Description("查询方案ID")
    @ApiOperation(value = "查询方案ID")
    public Resp<Long> getPlanId(@ApiParam("表单ID：值") Map<Long, ProcessInfoSubmit> processInfoSubmitMap, @ApiParam("流程ID") String approvalProcessId){
        return new Resp().success(applyApprovalService.getPlanId(getLoginUserId(), processInfoSubmitMap, approvalProcessId));
    }
}
